<template>
  <div>
    <!-- 显示当前评论的cell -->
    <van-cell>
        <div style="display:flex; justify-content:space-between;">
          <div class="left-box">
            <van-image :src="item.aut_photo" round fit="cover" />
            <div class="info-box">
              <div class="name">{{ item.aut_name }}</div>
              <div class="content">{{ item.content }}</div>
              <div class="time">
                {{ $relvTime(item.pubdate) }}
              </div>
            </div>
          </div>
        </div>
      </van-cell>

    <!-- 分割线 -->
    <van-divider>回复列表</van-divider>

    <!-- 列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <van-cell v-for="item in list" :key="item.com_id">
        <div style="display:flex; justify-content:space-between;">
          <div class="left-box">
            <van-image :src="item.aut_photo" round fit="cover" />
            <div class="info-box">
              <div class="name">{{ item.aut_name }}</div>
              <div class="content">{{ item.content }}</div>
              <div class="time">
                {{ $relvTime(item.pubdate) }}

              </div>
            </div>
          </div>

        </div>
      </van-cell>
    </van-list>

    <!-- 输入框 -->
    <div class="cmt-box">
      <van-field
        v-model="cmt"
        center
        clearable
        placeholder="请输入评论内容"
        @keyup.enter="send"
      >
        <template #button>
          <van-button size="mini" type="info" @click="send">发布</van-button>
        </template>
      </van-field>
    </div>
  </div>
</template>

<script>
import { cmtListAPI, sendAPI } from '@/api/comment'
export default {
  props: {
    item: {
      type: Object,
      required: true
    }
  },
  data () {
    return {

      cmt: '',
      list: [],
      loading: false,
      finished: false,
      offset: null
    }
  },
  methods: {
    async onLoad () {
      const res = await cmtListAPI({
        // 获取回复就传c
        type: 'c',
        // 获取回复就传评论id，代表获取哪篇评论的回复
        source: this.item.com_id,
        // null就是相当于没传，没传就是从第一页开始查
        offset: this.offset,
        limit: 10
      })

      // 本次的最后一条id
      this.offset = res.data.last_id

      // 把数据加到数组里
      this.list.push(...res.data.results)
      // 加载完本次记得把loading改为false
      this.loading = false

      // 判断是否查完了
      if (this.list.length >= res.data.total_count) {
        this.finished = true
      }
    },

    // 发布
    async send () {
      // 1. 非空判断
      if (this.cmt === '') return this.$toast('请输入内容')

      // 2. 有内容就要发请求给服务器
      const res = await sendAPI({
        // 传当前评论的id, 我们要发的是回复
        target: this.item.com_id,
        content: this.cmt,
        // 如果要发回复，必须传文章id
        art_id: this.$route.params.id
      })

      // 3. 输入内容清空
      this.cmt = ''

      // 4. 把新内容渲染出来
      this.list.unshift(res.data.new_obj)

      // 新增了一条回复，就要让当前评论数量里的reply_count+1
      this.$emit('add')
    }
  }
}
</script>

<style></style>
